package cn.qrx.gateway.datasource.unpooled;

import cn.qrx.gateway.datasource.Connection;
import cn.qrx.gateway.datasource.DataSource;
import cn.qrx.gateway.datasource.DataSourceType;
import cn.qrx.gateway.datasource.connection.DubboConnection;
import cn.qrx.gateway.mapping.HttpStatement;
import cn.qrx.gateway.seesion.Configuration;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

/**
 * @Author: 屈燃希
 * @Date: 2025/10/20
 * @Version: V1.0
 * @Description:
 */
public class UnpooledDataSource implements DataSource {

    private Configuration configuration;

    private HttpStatement httpStatement;

    private DataSourceType dataSourceType;
    @Override
    public Connection getConnection() {
        switch (dataSourceType)
        {
            case HTTP:
                break;
            case DUBBO:
                String application = httpStatement.getApplication();
                String interfaceName = httpStatement.getInterfaceName();

                // 获取基础服务（创建成本较高，内存存放获取）
                ApplicationConfig applicationConfig = configuration.getApplicationConfig(application);
                RegistryConfig registryConfig = configuration.getRegistryConfig(application);
                ReferenceConfig<GenericService> reference = configuration.getReferenceConfig(interfaceName);
                return  new DubboConnection(applicationConfig, registryConfig, reference);
        }
        throw new RuntimeException("DataSourceType：" + dataSourceType + "没有对应的数据源实现");
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public void setHttpStatement(HttpStatement httpStatement) {
        this.httpStatement = httpStatement;
    }

    public void setDataSourceType(DataSourceType dataSourceType) {
        this.dataSourceType = dataSourceType;
    }
}
